<%@page import="org.apache.commons.fileupload.util.Streams"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.BufferedOutputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="org.apache.commons.fileupload.FileItemStream"%>
<%@page import="org.apache.commons.fileupload.FileItemIterator"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String fileName = "";  //上传文件的文件名
	File tmpDir = new File("d:\\temp");
	String file = "";   //文件的生成路径名
	String showFileName = null;  //显示的文件名
	String showImageUrl = "";  //上传文件的图标路径
	try{
		if (ServletFileUpload.isMultipartContent(request)){
			DiskFileItemFactory factory = new DiskFileItemFactory();
			factory.setSizeThreshold(1 * 1024 * 1024);
			factory.setRepository(tmpDir);
			ServletFileUpload sfu = new ServletFileUpload(factory);
			sfu.setFileSizeMax(20 * 1024 * 1024);
			sfu.setSizeMax(20 * 1024 * 1024);
			sfu.setHeaderEncoding("UTF-8");
			FileItemIterator fii = sfu.getItemIterator(request);
			String uploadPath = getServletContext().getRealPath("/") + "upload";//获取文件路径  
			System.out.println(uploadPath);
			
			if (!new File(uploadPath).isDirectory()) {
				new File(uploadPath).mkdirs();
			}
			while(fii.hasNext()){
				FileItemStream fis = fii.next();
				if(!fis .isFormField() && fis.getName().length() > 0){
					fileName = fis.getName();
					BufferedInputStream in = new BufferedInputStream(fis.openStream());
					BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(new File(uploadPath + File.separator + fileName)));
					Streams.copy(in, os, true);
					in.close();
					os.close();
					file = "upload/" + fileName;
				}
			}
		}
		response.getWriter().write("<script>parent.callback('"+file+"')</script>");
	}catch (Exception e) {
		e.printStackTrace();
	}
%>